﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using Infrastructure;

namespace MSIACCSL2.Web.ReportPage
{
    public partial class Neraca : System.Web.UI.Page
    {
        private DataAccess<Object> DA;
        private ReportDocument neraca;

        public Neraca()
        {
            DA = new DataAccess<object>();
            neraca = new ReportDocument();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
          if (!Page.IsPostBack)
          {
              LoadBulan();
              cboBulan.SelectedValue = DateTime.Now.Month.ToString();
              txtTahun.Text = DateTime.Now.Year.ToString();

          }else
          {
              LoadReport();
          }
           
        }

        public void LoadReport()
        {
           
                StringBuilder sql = new StringBuilder();
                using (StreamReader sr = File.OpenText(Server.MapPath("~/sql/ReportNeraca.txt")))
                {
                    String str = string.Empty;
                    while ((str = sr.ReadLine()) != null)
                        sql.Append(str);
                }

                var tglStart = new DateTime(int.Parse(txtTahun.Text), int.Parse(cboBulan.SelectedValue), 1);
                var tglEnd = new DateTime(int.Parse(txtTahun.Text), int.Parse(cboBulan.SelectedValue), DateTime.DaysInMonth(int.Parse(txtTahun.Text), int.Parse(cboBulan.SelectedValue)));

                sql.Replace("@param1", tglStart.ToString("yyyy-MM-dd"));
                sql.Replace("@param2", tglEnd.ToString("yyyy-MM-dd"));


                var Source = DA.GetDataListDataSet(sql.ToString());
                string reportPath = string.Empty;
                if (rblTipe.SelectedValue == "Detail")

                    reportPath = Server.MapPath("~/Report/ReportNeraca.rpt");
                else
                    reportPath = Server.MapPath("~/Report/NeracaSummary.rpt");

                neraca.Load(reportPath);
                neraca.SetDataSource(Source.Tables[0]);
                SetupParameter(tglStart, tglEnd, neraca);
                CrystalReportViewer1.ReportSource = neraca;
                CrystalReportViewer1.PrintMode = CrystalDecisions.Web.PrintMode.ActiveX;
        }

        private void SetupParameter(DateTime tglStart, DateTime tglEnd, ReportDocument reportDocument)
        {
            var param = new ParameterDiscreteValue();
            param.Value = tglStart.ToString("dd-MM-yyyy");
            var paramValue = new ParameterValues();
            paramValue.Add(param);
            reportDocument.DataDefinition.ParameterFields["tglawal"].ApplyCurrentValues(paramValue);
            var param2 = new ParameterDiscreteValue();
            param2.Value = tglEnd.ToString("dd-MM-yyyy");
            var paramValue2 = new ParameterValues();
            paramValue2.Add(param2);
            reportDocument.DataDefinition.ParameterFields["tglakhir"].ApplyCurrentValues(paramValue2);
        }

        void LoadBulan()
        {
            cboBulan.Items.Add(new ListItem("Januari","1"));
            cboBulan.Items.Add(new ListItem("Februari", "2"));
            cboBulan.Items.Add(new ListItem("Maret", "3"));
            cboBulan.Items.Add(new ListItem("April", "4"));
            cboBulan.Items.Add(new ListItem("Mei", "5"));
            cboBulan.Items.Add(new ListItem("Juni", "6"));
            cboBulan.Items.Add(new ListItem("Juli", "7"));
            cboBulan.Items.Add(new ListItem("Agustus", "8"));
            cboBulan.Items.Add(new ListItem("September", "9"));
            cboBulan.Items.Add(new ListItem("Oktober", "10"));
            cboBulan.Items.Add(new ListItem("November", "11"));
            cboBulan.Items.Add(new ListItem("Desember", "12"));
        }

        protected void btnShow_Click(object sender, EventArgs e)
        {
            LoadReport();
        }

        protected override void OnUnload(EventArgs e)
        {
            base.OnUnload(e);
            this.Unload += Page_Unload;
        }

        void Page_Unload(object sender, EventArgs e)
        {
            neraca.Clone();
            neraca.Dispose();

        }

        protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
        {
            int res = 0;
            args.IsValid = int.TryParse(cboBulan.SelectedValue,out res) && int.TryParse(txtTahun.Text,out res);
        }
     
    }
}